package com.skywolf.chem.match.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.skywolf.chem.common.config.data.DataSource;
import com.skywolf.chem.common.config.data.DataSourceNames;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.match.entity.ApplyUserInfo;
import com.skywolf.chem.match.mapper.ApplyUserInfoMapper;
import com.skywolf.chem.match.params.param.ApplyUserInfoHistoryParam;
import com.skywolf.chem.match.params.param.ApplyUserInfoParam;
import com.skywolf.chem.match.service.IApplyUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.match.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-09-26
 */
@Service
public class ApplyUserInfoServiceImpl extends ServiceImpl<ApplyUserInfoMapper, ApplyUserInfo> implements IApplyUserInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DataSource(DataSourceNames.TWO)
    public void updateHistoryUser(Long userId, ApplyUserInfoHistoryParam param) throws Exception {
        if (param.getId() == null) {
            throw new BizException("更新时id不能为空！");
        }
        ApplyUserInfo info = new ApplyUserInfo();
        BeanUtils.copyProperties(param, info);
        info.setUserId(userId);
        this.updateById(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DataSource(DataSourceNames.TWO)
    public void createHistoryUser(Long userId, ApplyUserInfoHistoryParam param) {
        ApplyUserInfo info = new ApplyUserInfo();
        BeanUtils.copyProperties(param, info);
        info.setUserId(userId);
        this.save(info);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public void removeById(Long id) {
        this.removeById(id);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public List<ApplyUserInfoHistoryParam> getList(Long userId) {
        List<ApplyUserInfo> infos = this.list(new LambdaQueryWrapper<ApplyUserInfo>().eq(ApplyUserInfo::getUserId, userId));
        return null == infos || infos.size() == 0 ? new ArrayList<ApplyUserInfoHistoryParam>() :
                infos.parallelStream().map(item -> {
                    ApplyUserInfoHistoryParam param = new ApplyUserInfoHistoryParam();
                    BeanUtils.copyProperties(item, param);
                    return param;
                }).collect(Collectors.toList());
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public ApplyUserInfoHistoryParam getInfo(Long id) throws Exception {
        ApplyUserInfo info = this.getById(id);
        if (null == info) {
            throw new BizException("当前通讯录用户不存在！");
        }
        ApplyUserInfoHistoryParam param = new ApplyUserInfoHistoryParam();
        BeanUtils.copyProperties(info, param);
        return param;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveApplyUser(Long userId, ApplyUserInfoParam param) {
        ApplyUserInfo info = this.getOne(new LambdaQueryWrapper<ApplyUserInfo>().eq(ApplyUserInfo::getUserId, userId)
                .eq(ApplyUserInfo::getName, param.getName())
                .eq(ApplyUserInfo::getIdCard, param.getIdCard()));
        if (!StringUtils.isEmpty(info)) {
            ApplyUserInfo updateInfo = new ApplyUserInfo();
            BeanUtils.copyProperties(param, updateInfo);
            updateInfo.setUserId(userId);
            updateInfo.setId(info.getId());
            this.updateById(updateInfo);
        } else {
            info = new ApplyUserInfo();
            BeanUtils.copyProperties(param, info);
            info.setUserId(userId);
            this.save(info);
        }
    }
}
